﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using BoundaryLightLayout;
using YArchitech.Controls;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using YJKArch.Utils;
using HYCodeBase.WinForm;
using HYView;
using LightingEquipmentLay.RegionSearch;
using LightingEquipmentLay.Utility;
using RegionSplittor.Utils;
using SelectTypeClass;

namespace HYLightingEquipmentLay
{
	[Transaction(TransactionMode.Manual)]
	public class CmdTestLaneLightingLayout : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			if (SwitchView.SetUsableView(CmdViewType.FloorPlan, commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			try
			{
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Document linkedDocByCurDoc = LinkDocumentOperator.GetLinkedDocByCurDoc(document, "A");
				Document linkedDocByCurDoc2 = LinkDocumentOperator.GetLinkedDocByCurDoc(document, "S");
				if (linkedDocByCurDoc2 == null || linkedDocByCurDoc == null)
				{
					YJKMessageBox.Show("项目中不存在建筑或结构的链接文件！");
					return Autodesk.Revit.UI.Result.Failed;
				}
				commandData.Application.Application.FailuresProcessing += DeleteWarning.OnFailuresProcessing;
				List<CurveLoop> userSelectedLoops = CurveloopsSelector.GetUserSelectedLoops(commandData, linkedDocByCurDoc);
				if (userSelectedLoops.Count < 1)
				{
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				LightingLayoutOper.InitLaneLightStyle();
				GarageLaySettingAuxiliary garageLaySettingAuxiliary = new GarageLaySettingAuxiliary();
				string empty = string.Empty;
				FamilySymbol layLight = garageLaySettingAuxiliary.GetLayLight(document, ref empty);
				if (layLight == null)
				{
					YJKMessageBox.Information(empty);
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				Level genLevel = document.ActiveView.GenLevel;
				LightingLayoutOper.m_strLightLayStyle = garageLaySettingAuxiliary.GetGarageLineInterfaceInfo().LightLayStyle;
				List<KeyValuePair<LightingLayoutOper.ComparableLine, List<XYZ>>> lineIntersectionInfos = LightingLayoutOper.GetLineIntersectionInfos(RegionSearchManager.InitLinearCombineLaneData(linkedDocByCurDoc, genLevel));
				List<Curve> lstLanes = RegionSearchManager.InitLaneRegionData(linkedDocByCurDoc, genLevel);
				List<FamilyInstance> lstParkings = RegionSearchManager.InitParkingRegionData(linkedDocByCurDoc, genLevel);
				List<Room> lstRooms = RegionSearchManager.InitRoomRegionData(linkedDocByCurDoc, genLevel);
				List<RegionFuncInfo> docBeamCellFuncInfos = RegionSearchManager.GetDocBeamCellFuncInfos(activeUIDocument, genLevel, userSelectedLoops, lstLanes, lstParkings, lstRooms);
				List<RegionFuncInfo> beamLaneRegions = (from e in docBeamCellFuncInfos
				where e.ERegionFunc == ERegionFunc.eLane
				select e).ToList<RegionFuncInfo>();
				List<LightingLayoutOper.LightingInfo> list = LightingLayoutOper.DoLightingLayoutByLanes(genLevel, beamLaneRegions, lineIntersectionInfos, true);
				using (ProgressBarForm progressBarForm = new ProgressBarForm(list.Count, ProgressBarStyle.Blocks, true))
				{
					WindowHandle owner = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
					progressBarForm.Text = "正在进行车道线布灯操作";
					progressBarForm.ShowInTaskbar = false;
					progressBarForm.Show(owner);
					foreach (LightingLayoutOper.LightingInfo lightingInfo in list)
					{
						progressBarForm.RefreshProgressBar();
						if (progressBarForm.m_stopTag)
						{
							return Autodesk.Revit.UI.Result.Succeeded;
						}
						LightingEquipmentLay.Utility.Common.CreateLightingFamilyInstance(document, lightingInfo, lightingInfo.Angle, layLight);
					}
				}
				List<LightingLayoutOper.LightingInfo> list2 = LightingLayoutOper.DoLightingLayoutByParkings((from e in docBeamCellFuncInfos
				where e.ERegionFunc == ERegionFunc.eParking
				select e).ToList<RegionFuncInfo>());
				using (ProgressBarForm progressBarForm2 = new ProgressBarForm(list2.Count, ProgressBarStyle.Blocks, true))
				{
					WindowHandle owner2 = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
					progressBarForm2.Text = "正在进行车位布灯操作";
					progressBarForm2.ShowInTaskbar = false;
					progressBarForm2.Show(owner2);
					foreach (LightingLayoutOper.LightingInfo lightingInfo2 in list2)
					{
						progressBarForm2.RefreshProgressBar();
						if (progressBarForm2.m_stopTag)
						{
							return Autodesk.Revit.UI.Result.Succeeded;
						}
						LightingEquipmentLay.Utility.Common.CreateLightingFamilyInstance(document, lightingInfo2, lightingInfo2.Angle, layLight);
					}
				}
				List<RegionFuncInfo> list3 = (from e in docBeamCellFuncInfos
				where e.ERegionFunc == ERegionFunc.eOthers
				select e).ToList<RegionFuncInfo>();
				using (ProgressBarForm progressBarForm3 = new ProgressBarForm(list3.Count, ProgressBarStyle.Blocks, true))
				{
					WindowHandle owner3 = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
					progressBarForm3.Text = "正在进行边角区域布灯操作";
					progressBarForm3.ShowInTaskbar = false;
					progressBarForm3.Show(owner3);
					double dIllumStandard = 30.0;
					double dIllumFlex = 2500.0;
					double dUserFactor = 0.6;
					double dLLF = 0.7;
					Plane plane = UnifiedModified.CreatePlane(XYZ.BasisZ, XYZ.Zero);
					foreach (RegionFuncInfo regionFuncInfo in list3)
					{
						progressBarForm3.RefreshProgressBar();
						if (progressBarForm3.m_stopTag)
						{
							return Autodesk.Revit.UI.Result.Succeeded;
						}
						List<Curve> list4 = regionFuncInfo.BoundaryCvs.Cast<Curve>().ToList<Curve>();
						ushort nLightCount = LightingEquipmentLay.Utility.Common.RoomIllumCalcLampCount(Geometry.CurveAoArea(list4) * 304.8 * 304.8 / 1000.0 / 1000.0, dIllumStandard, dIllumFlex, dUserFactor, dLLF);
						LightLayoutOper lightLayoutOper = new LightLayoutOper(document, list4, plane);
						lightLayoutOper.DoLightLayout(nLightCount);
						XYZ vtColOffset = lightLayoutOper.VtColOffset;
						double num = 0.0;
						num += XYZ.BasisX.AngleOnPlaneTo(vtColOffset, XYZ.BasisZ);
						foreach (BoundaryGridRowData boundaryGridRowData in lightLayoutOper.GridRowDatas)
						{
							foreach (BoundaryGridSegmentData boundaryGridSegmentData in boundaryGridRowData.GridSegmentDatas)
							{
								foreach (XYZ pt in boundaryGridSegmentData.Points)
								{
									LightingEquipmentLay.Utility.Common.CreateLightingFamilyInstance(document, pt, num, layLight);
								}
							}
						}
					}
				}
			}
			catch (Exception)
			{
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
